package org.Panel;

import org.JsonMsg.Post;
import org.implement.Panel.PanelSize;
import org.implement.Panel.PanelSkip;
import org.monitor.PanelMonitor;

import javax.swing.*;
import java.util.HashMap;
import java.util.Map;

public class MainPanel extends JFrame implements PanelSize , PanelSkip {

    //窗口大小
    public static int Width = 1200;
    public static int High = 800;

    //界面监测器
    PanelMonitor monitor;

    //创建开始界面
    PlayPanel playPanel;

    //界面id的键值对
    Map<Integer,FaPanel> panelMap = new HashMap<Integer,FaPanel>();

    //显示界面
    public FaPanel focusedPanel;

    //登陆界面
    LoginPanel loginPanel;

    //注册界面
    RegisterPanel registerPanel;

    //个人界面
    PersonalPanel personalPanel;

    //贴吧主页
    MainPostPanel mainPostPanel;

    //通用帖子面板
    PostPanel postPanel;

    //帖子操作界面
    PostOP postOP;

    //当前帖子
    public static Post post;
    //当前用户id
    public static int userId;
    //当前用户名
    public static String userName = "svc";
    //全部帖子信息
    public static String AllPost = "";
    //登陆的用户的管理权限
    public static String userPower = "";

    //基座面板
    public FaPanel BasePanel = new FaPanel(){
        public void Run() {
            this.setBounds(0,0,getWidth(),getHeight());
        }
    };

    public MainPanel() {
        init();

        initpanelMap();

    }

    //初始化界面id键值对
    private void initpanelMap() {

//        panelMap.put(playPanel.PanelIndex, new FaPanel());

    }

    private void init() {
        //窗口属性设置
        setTitle("HUFE贴吧");
        setSize(Width, High);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(null);

        //基座面板属性设置
        BasePanel.setLayout(null);
        BasePanel.setSize(getWidth(), getHeight());
        //将基座界面放入窗口
        add(BasePanel);

        //创建开始界面
        playPanel = new PlayPanel(this,this);
        playPanel.setSize(0,0);
        addPanel(playPanel);

        //登陆界面
        loginPanel = new LoginPanel(this,this);
        loginPanel.setSize(0,0);
        addPanel(loginPanel);

        //注册界面
        registerPanel = new RegisterPanel(this,this);
        registerPanel.setSize(0,0);
        addPanel(registerPanel);

        //个人界面
        personalPanel = new PersonalPanel(this,this);
        personalPanel.setSize(0,0);
        addPanel(personalPanel);

        //贴吧主页
        mainPostPanel = new MainPostPanel(this,this);
        mainPostPanel.setSize(0,0);
        addPanel(mainPostPanel);

        //通用帖子面板
        postPanel = new PostPanel(this,this);
        postPanel.setSize(0,0);
        addPanel(postPanel);

        //帖子操作界面
        postOP = new PostOP(this,this);
        postOP.setSize(0,0);
        addPanel(postOP);

        //显示界面
        focusedPanel = playPanel;

        //将要显示的界面至于顶层
        BasePanel.setComponentZOrder(focusedPanel,BasePanel.getComponentCount()-1);

        //窗口显示
        setVisible(true);
        focusedPanel.setBounds(0,0,getWidth(),getHeight());

        //创建界面监测器
        monitor = new PanelMonitor(this);
        //启动监测器
        monitor.start();

    }

    //添加控件至基座界面
    public void addPanel (FaPanel panel) {
        panelMap.put(panel.PanelIndex,panel);
        BasePanel.add(panel);
        panel.setSize(0,0);
    }

    //跳转界面方法
    @Override
    public void skip(int panelid) {

        //更新监测器的监测界面为跳转到的界面
        monitor.setPanels(panelMap.get(panelid), BasePanel);
        //将当前界面大小归零
        focusedPanel.setBounds(0,0,0,0);
        //通过传入的索引找到对应界面的引用
        focusedPanel = panelMap.get(panelid);
        //将要跳转到的界面大小放大到和窗口一样大
        focusedPanel.setBounds(0,0,getWidth(),getHeight());
        //将要跳转到的界面置顶
        BasePanel.setComponentZOrder(focusedPanel,BasePanel.getComponentCount()-1);

        //进入界面后更新界面
        focusedPanel.updatePanel();

    }
}
